<template>
  <div class="banner">
    <NuxtImg :src="picture" class="img-responsive" alt="#" />

    <!-- 页面标题显示区域 -->
    <div v-if="name" class="page-title">
      <div class="liner"></div>
      <div class="title-decoration">
        <h1 class="title-text">{{ name }}</h1>
      </div>
    </div>

    <div class="position">
      <UIcon name="uil:map-marker" class="mr-2 sectionIcon"></UIcon>
      <span>当前位置:</span>
      <template v-for="(item, index) in breadcrumbPath" :key="index">
        <span v-if="index > 0" class="separator"> >> </span>
        <NuxtLink :to="item.link" class="breadcrumb-link">
          {{ item.name }}
        </NuxtLink>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
/**
 * 位置导航组件，根据menuCode显示面包屑导航
 * @param menuCode - 菜单代码，用于查找对应的菜单路径
 */
const appConfig = useAppConfig();
const { menus } = appConfig.systemConfig;

const props = defineProps({
  menuCode: {
    type: String,
    default: ""
  },
  picture: {
    type: String,
    default: ""
  },
  name: {
    type: String,
    default: ""
  }
});

/**
 * 递归查找菜单路径
 * @param menuList - 菜单列表数组
 * @param targetCode - 目标菜单代码
 * @param currentPath - 当前路径数组
 * @returns 找到的菜单路径数组或null
 */
function findMenuPath(menuList: any[], targetCode: string, currentPath: any[] = []): any[] | null {
  for (const menu of menuList) {
    const newPath = [...currentPath, { name: menu.name, link: menu.link, code: menu.code }];

    if (menu.code === targetCode) {
      return newPath;
    }

    if (menu.children && menu.children.length > 0) {
      const result = findMenuPath(menu.children, targetCode, newPath);
      if (result) {
        return result;
      }
    }
  }
  return null;
}

/**
 * 计算面包屑路径
 * @returns 包含首页和菜单路径的完整面包屑数组
 */
const breadcrumbPath = computed(() => {
  // 固定的首页
  const homePath = [{ name: "首页", link: "/", code: "home" }];

  if (!props.menuCode) {
    return homePath;
  }

  const menuPath = findMenuPath(menus, props.menuCode);
  if (menuPath) {
    return [...homePath, ...menuPath];
  }

  return homePath;
});
</script>

<style scoped lang="scss">
.banner {
  position: relative;

  .img-responsive {
    width: 100%;
    // height: 392px;
    max-height: 392px;
    min-height: 200px;
    object-fit: cover;
    object-position: center;
  }

  // banner底部连接条
  &::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 10px;
    background-color: rgba($custom-primary, 0.8);
    z-index: 1;
  }

  // 页面标题样式
  .page-title {
    .liner {
      width: 40px;
      height: 2px;
      background-color: #fff;
      margin-bottom: 20px;
    }
    position: absolute;
    width: 100%;
    max-width: 1400px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 3;

    .title-decoration {
      // background-color: rgba($custom-primary, 0.8);
      border-radius: 4px;

      .title-text {
        color: #fff;
        font-size: 22px;
        font-weight: 600;
        margin: 0;
      }
    }

    // 移动端适配
    @media (max-width: 767px) {
      padding: 0 12px;
      .title-decoration {
        .title-text {
          font-size: 18px;
        }
      }
      .liner {
        margin-bottom: 4px;
      }
    }
  }

  .position {
    position: absolute;
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 14px;
    color: #fff;
    background-color: rgba($custom-primary, 0.8);
    padding: 12px 20px;
    z-index: 2;

    // PC端：右下角定位，左侧斜边剪切
    @media (min-width: 768px) {
      bottom: 10px;
      right: 0;
      clip-path: polygon(20px 0, 100% 0, 100% 100%, 0 100%);
      min-width: 200px;
      width: auto;
    }

    // 移动端：左上角定位，无剪切
    @media (max-width: 767px) {
      bottom: 10px;
      left: 0;
      width: auto;
      padding: 4px 12px;
      font-size: 12px;
    }
  }
}

.separator {
  color: rgba(255, 255, 255, 0.8);
}

.breadcrumb-link {
  color: #fff;
  text-decoration: none;
  transition: color 0.2s;
  font-weight: 500;
}

.breadcrumb-link:hover {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: underline;
}
</style>
